package com.sangmo.fts.process.inventory.service;

import com.sangmo.boot.framework.common.utils.DateUtils;
import com.sangmo.boot.framework.data.idm.IdentityManager;
import com.sangmo.boot.framework.plugin.dataview.crud.service.BaseViewService;
import com.sangmo.boot.framework.plugin.dataview.data.IDataViewRepo;
import com.sangmo.fts.basic.service.FileUploadService;
import com.sangmo.fts.common.authority.service.AuthorityEntityService;
import com.sangmo.fts.process.inventory.interfaces.IfStockCreation;
import com.sangmo.fts.process.inventory.model.IfStock;
import com.sangmo.fts.process.inventory.model.IfStockItem;
import com.sangmo.fts.process.inventory.model.IfWaybill;
import com.sangmo.fts.process.inventory.model.IfWaybillItem;
import com.sangmo.fts.process.inventory.repo.IfStockRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class IfStockEntityService extends AuthorityEntityService<IfStock> {

    @Autowired
    private IfStockRepo ifStockRepo;

    @Autowired
    private IdentityManager identityManager;

    @Autowired
    private FileUploadService fileUploadService;

    @Autowired
    private IfWaybillService ifWaybillService;

    @Autowired
    private IfStockItemEntityService ifStockItemEntityService;

    @Override
    protected IDataViewRepo<IfStock> getRepo() {
        return ifStockRepo;
    }

    @Override
    protected void onCreate(IfStock entity, Object bo) {
        super.onCreate(entity, bo);
//        entity.setCode(DateUtils.format(new Date(), "yyyyMMddHHmmss") + identityManager.next("IF_STOCK", 4));
        IfStockCreation creation = (IfStockCreation) bo;
        IfWaybill waybill = ifWaybillService.get(creation.getWaybillId());
        entity.setStorager(waybill.getShipper());
        BigDecimal goodsTotalAmount = ifWaybillService.calculateGoodsTotalAmount(waybill.getId());
        entity.setGoodsTotalAmount(goodsTotalAmount);
        if (null != creation.getConfirmDoc()) {
            fileUploadService.updateRef(creation.getConfirmDoc(), ""+entity.getId(), "IfStock");
        }
    }

    @Override
    protected void afterCreated(IfStock entity, Object bo) {
        super.afterCreated(entity, bo);
        IfStockCreation creation = (IfStockCreation) bo;
        List<IfWaybillItem> waybillItems = ifWaybillService.listItemsByWaybillId(creation.getWaybillId());
        List<IfStockItem> stockItems = buildIfStockItems(entity.getId(), waybillItems);
        ifStockItemEntityService.saveAll(stockItems);
    }

    private List<IfStockItem> buildIfStockItems(Long stockId, List<IfWaybillItem> waybillItems) {
        List<IfStockItem> stockItems = new ArrayList<>();
        for (IfWaybillItem waybillItem : waybillItems) {
            IfStockItem item = IfStockItem.fromWaybillItem(waybillItem);
            item.setStockId(stockId);
            stockItems.add(item);
        }
        return stockItems;
    }

    protected IfStock findByWaybillId(Long waybillId) {
        return ifStockRepo.findByWaybillId(waybillId);
    }
}
